﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdeManagmente.Model;
using ProdeManagmente.DAO;
using ProdeManagmente.Libs;

namespace ProdeManagmente.Forms.ABMS
{
    public partial class FrmTeam : Form
    {
        private Team team;
        private TeamService teamService;
        private bool changed = false;

        public FrmTeam(Team team)
        {
            InitializeComponent();
            this.team = team;            
            loadData();
            changedValue(false);
        }

        private void loadData()
        {
            if (team == null)
                return;

            txtId.Text = team.getId().ToString();
            txtName.Text = team.getName();
            txtLeague.Text = team.getLeague();
        }

        private void blindData()
        {
            if (team == null)
                team = new Team();
            team.setName(txtName.Text);
            team.setLeague(txtLeague.Text);
        }

        private void changeTitle()
        {
            String title = "Equipo";
            if (changed)
            {
                title = title + "*";
            }
            this.Text = title;
        }

        private void changedValue(bool value)
        {
            changed = value;
            changeTitle();
        }

        private void save()
        {
            teamService = TeamService.getTeamServiceInstance();
            try
            {
                blindData();
                teamService.save(this.team);
                changedValue(false);
                lblInfo.Text = "Operacion exitosa";
            }
            catch (BusinessException error)
            {
                new MensajeSalida(error).showMensaje();
            }
        }

        private bool exit()
        {
            if (changed)
            {
                DialogResult result = MessageBox.Show("Desea grabar los cambios?", "Salir", MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                {
                    save();
                    return false;
                }
                else if (result == DialogResult.No)
                {
                    changedValue(false);
                    return false;
                }
                else if (result == DialogResult.Cancel)
                {
                    return true;
                }
            }
            return false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            save();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void txtLeague_TextChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!exit())
                this.Close();
        }

        private void FrmTeam_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = exit();
        }
    }
}
